package com.elaine.factory.alapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.elaine.constant.AlapiConstant;
import com.elaine.model.alapi.AlapiResponse;
import com.elaine.model.alapi.BingData;
import com.elaine.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * @author wrb
 * @date 2020/4/1 13:56
 */
@Slf4j
@Component
public class BingAlapiFactory extends AlapiFactory {

	private static final String URL = "https://v1.alapi.cn/api/bing?format=json";

	@Override
	protected void registerFactory() {
		doRegister(AlapiConstant.BING, this);
	}

	@Override
	public String getReply() {
		String jsonStr = HttpUtils.requestGet(URL, null, null);
		AlapiResponse<BingData> alapiResponse = JSON.parseObject(jsonStr, new TypeReference<AlapiResponse<BingData>>() {
		});
		if (alapiResponse != null && alapiResponse.isSuccess()) {
			return super.getImageMediaIdByKey(alapiResponse.getData().getTh(), alapiResponse.getData().getUrl(), AlapiConstant.BING.name());
		} else {
			throw new IllegalArgumentException("必应美图请求失败咯，wrwrwr...");
		}
	}

}
